<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Traits\Base;
use App\Services\ChartAnalysis\ChartAnalysis;
use App\Services\ChartAnalysis\Data;
use App\Models\Flaw\Level;
use App\Services\ChartAnalysis\Algorithm;

class ExportController extends Controller
{
    use Base;
    //缓存实时更新开关
    protected  $cache;

    /**
     * exportVuls 导出筛选出来的数据
     */
    public function info(Request $request)
    {
        $pid = intval($request->pid) ? : 1;
        $service = new ChartAnalysis();
        //时间参数
        if ($request->startTime && $request->endTime) {
            $service->data->timeVaild = true;
            $service->data->startime  = $request->startTime;
            $service->data->endTime   = $request->endTime;
        }
        $time = [$request->startTime,$request->endTime];
        //初始化板块信息
        $service->data->getComposition($pid);
        $block = $service->data->ogz[$pid];
        $excelData = [];
        //计算
        $algorithm = new Algorithm();
        foreach ($block as $id => $name) {
            //新增情况
            $findCount = 0;
            $findCount += $serious   = $service->data->getFindNum([
                Level::SERIOUSID
            ],$id);
            $findCount += $secondary = $service->data->getFindNum([
                Level::HIGHRISKID
            ],$id);
            $findCount += $inriskid  = $service->data->getFindNum([
                Level::INRISKID
            ],$id);
            $findCount += $low       = $service->data->getFindNum([
                Level::LOWID
            ],$id);
            //修复情况
            $confirmedCount = 0;
            $confirmedCount += $seriousConfirmed  = $service->data->getConfirmedNumByLevel(Level::SERIOUSID);
            $confirmedCount += $secondaryfirmed   = $service->data->getConfirmedNumByLevel(Level::HIGHRISKID);
            $confirmedCount += $inriskidConfirmed = $service->data->getConfirmedNumByLevel(Level::INRISKID);
            $confirmedCount += $lowConfirmed      = $service->data->getConfirmedNumByLevel(Level::LOWID);
            //修复用时
            $seriousTime   = $algorithm->confirmUsedTime(Level::SERIOUSID,$service->data->getAppIds(),$time);
            $secondaryTime = $algorithm->confirmUsedTime(Level::HIGHRISKID,$service->data->getAppIds(),$time);
            $inriskidTime  = $algorithm->confirmUsedTime(Level::INRISKID,$service->data->getAppIds(),$time);
            $lowTime       = $algorithm->confirmUsedTime(Level::LOWID,$service->data->getAppIds(),$time);

            //急时修复数
            $timelyCount = 0;
            $timelyCount += $seriousTimely   = $algorithm->confirmTimelyNum(Level::SERIOUSID,$service->data->getAppIds(),$time);
            $timelyCount += $secondaryTimely = $algorithm->confirmTimelyNum(Level::HIGHRISKID,$service->data->getAppIds(),$time);
            $timelyCount += $inriskidTimely  = $algorithm->confirmTimelyNum(Level::INRISKID,$service->data->getAppIds(),$time);
            $timelyCount += $lowTimely       = $algorithm->confirmTimelyNum(Level::LOWID,$service->data->getAppIds(),$time);

            //板块
            $excelData[$name] = [
                //漏洞数量
                [$findCount,$serious,$secondary,$inriskid,$low],
                //修复漏洞数
                [$confirmedCount,$seriousConfirmed,$secondaryfirmed,$inriskidConfirmed,$lowConfirmed],
                //平均修复用时
                [$seriousTime,$secondaryTime,$inriskidTime,$lowTime],
                //及时修复数量
                [$timelyCount,$seriousTimely,$secondaryTimely,$inriskidTimely,$lowTimely]
            ];
        }
        if ($request->startTime)
            $request->startTime = $request->startTime . "-";
        $excelTitle = "集团漏洞统计".$request->startTime . $request->endTime;
        Excel::create($excelTitle, function ($excel)use($excelData) {

            $excel->sheet('漏洞统计', function ($sheet)use($excelData) {

                $sheet->loadView('excel.export',array('blocks' => $excelData));


            });
        })->export('xls');
    }
}
